package com.example.bai12;

import java.util.ArrayList;

import com.example.adapter.NhanVienAdapter;
import com.example.object.NhanVien;
import com.example.object.PhongBan;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class XemDanhSachNhanVienActivity extends Activity{
	
	ListView listnv;
	ArrayList<NhanVien> arr;
	NhanVienAdapter adapter;
	Button btnback;
	NhanVien nvselect;
	PhongBan pb;
	int position=-1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xem_ds_nv);
		btnback= (Button) findViewById(R.id.btnback);
		listnv= (ListView) findViewById(R.id.listnv);
		getDataFromMain();
		addEvents();
		registerForContextMenu(listnv);
	}
	
	private void getDataFromMain()
	{
		Intent i= getIntent();
		Bundle b= i.getBundleExtra("DATA");
		 pb= (PhongBan) b.getSerializable("PHONGBAN");
		arr= pb.getList();
		adapter= new NhanVienAdapter(XemDanhSachNhanVienActivity.this,R.layout.layout_item_custom, arr);
		listnv.setAdapter(adapter);
	}
	private void addEvents()
	{
		btnback.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				proccessBack();
			}
		});
		listnv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				nvselect=arr.get(arg2);
				return false;
			}
		});
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.menunhanvien, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId())
		{
		case R.id.itemsuanv:
			proccessSuaNV();
			break;
		case R.id.itemchuyenpb:
			proccessChuyenPB();
			break;
		case R.id.itemxoanv:
			proccessXoaNV();
			break;
		}
		return super.onContextItemSelected(item);
	}
	
	private void proccessSuaNV()
	{
		Intent i= new Intent(this, SuaNhanVienActivity.class);
		Bundle b= new Bundle();
		b.putSerializable("NHANVIEN", nvselect); 
		i.putExtra("DATA", b);
		startActivityForResult(i, MainActivity.SUA_NHAN_VIEN);
	}
	
	private void proccessChuyenPB()
	{
		Intent i= new Intent(this, ChuyenPhongBanActivity.class);
		Bundle b= new Bundle();
		b.putSerializable("NHANVIEN", nvselect); 
		i.putExtra("DATA", b);
		startActivityForResult(i, MainActivity.CHUYEN_PHONG);
	}
	private void proccessXoaNV()
	{
		AlertDialog.Builder builder= new Builder(this);
		builder.setTitle("Hỏi");
		builder.setMessage("Thực sự muốn xóa ["+nvselect.getName()+"]??");
		builder.setPositiveButton("Có", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				arr.remove(nvselect);
				adapter.notifyDataSetChanged();
			}
		});
		builder.setNegativeButton("Không", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		builder.show();
	}
	private void proccessBack()
	{
		Intent i= getIntent();
		Bundle b= new Bundle();
		b.putSerializable("PHONGBAN",pb);
		i.putExtra("DATA", b);
		setResult(MainActivity.CAP_NHAT_DS_NV_THANH_CONG, i);
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode== MainActivity.CAP_NHAT_DS_NV_THANH_CONG)
		{
			Bundle b= data.getBundleExtra("DATA");
			NhanVien nv= (NhanVien) b.getSerializable("NHANVIEN");
			arr.set(position, nv);
			adapter.notifyDataSetChanged();
		}
		if(resultCode== MainActivity.CHUYEN_PHONG_THANH_CONG)
		{
			arr.remove(position);
			adapter.notifyDataSetChanged();
		}
	}
	

}
